package com.sjgs.gis.quartz;

import com.sjgs.gis.domain.ScheduleJob;
import com.sjgs.gis.domain.enumeration.JobOperateEnum;
import com.sjgs.gis.repository.ScheduleJobRepository;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.time.Duration;


@Service
public class ScheduleJobService {

    private static final Log logger = LogFactory.getLog(ScheduleJobService.class);

    @Autowired
    private QuartzService quartzService;

    private final ScheduleJobRepository scheduleJobRepository;

    public ScheduleJobService(ScheduleJobRepository scheduleJobRepository) {
        this.scheduleJobRepository = scheduleJobRepository;
    }

    Flux<ScheduleJob> findAll() {
        return scheduleJobRepository.findAll();
    }

    public void add(ScheduleJob job) {
        //此处省去数据验证
        scheduleJobRepository.save(job).subscribe(it -> {
            //加入job
            try {
                quartzService.addJob(it);
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
        });
    }

    public void start(long id) {
        //此处省去数据验证
        scheduleJobRepository.findById(id).subscribe(job -> {
            //执行job
            try {
                quartzService.operateJob(JobOperateEnum.START, job);
            } catch (SchedulerException e) {
                logger.error(e.getMessage());
            }
            job.setStatus(1);
            scheduleJobRepository.save(job).subscribe();
        });

    }

    public void pause(long id) {
        //此处省去数据验证
        scheduleJobRepository.findById(id).subscribe(job -> {
            //执行job
            try {
                quartzService.operateJob(JobOperateEnum.PAUSE, job);
            } catch (SchedulerException e) {
                logger.error(e.getMessage());
            }
            job.setStatus(2);
            scheduleJobRepository.save(job).subscribe();
        });
    }

    public void delete(ScheduleJob job) {
        //执行job
        try {
            quartzService.operateJob(JobOperateEnum.DELETE, job);
            //此处省去数据验证
            scheduleJobRepository.delete(job)
                    .delaySubscription(Duration.ofMillis(1000))
                    .subscribe();
        } catch (SchedulerException e) {
            logger.error(e.getMessage());
        }
    }

    public void deleteAllByName(String name) {
        Flux<ScheduleJob> jobs = scheduleJobRepository.findAllByJobName(name);
        jobs.subscribe(job -> {
            delete(job);
        });
    }

    public void startAllJob() {
        //执行job
        try {
            quartzService.startAllJob();
        } catch (SchedulerException e) {
            logger.error(e.getMessage());
        }
        //此处省去数据验证
        findAll().subscribe(job -> {
            job.setStatus(1);
            scheduleJobRepository.save(job).subscribe();
        });
    }

    public void pauseAllJob() {
        //执行job
        try {
            quartzService.pauseAllJob();
            //此处省去数据验证
            findAll().subscribe(job -> {
                job.setStatus(2);
                scheduleJobRepository.save(job).subscribe();
            });
        } catch (SchedulerException e) {
            logger.error(e.getMessage());
        }
    }
}
